import { delCache } from "#server/utils/context"; import { z } from "zod"; import { validate } from "#server/utils/validation"; import { updateCategory, getCategoryById } from "../../service/category"; const updateSchema = z.object({ name: z.string().min(1, "名称不能为空").max(100, "名称不能超过 100 字").optional(), slug: z .string() .min(1, "标识不能为空") .max(100) .regex(/^[a-z0-9]+(?:-[a-z0-9]+)*$/, "标识只能包含小写字母、数字和连字符") .optional(), image: z.string().max(500, "图片地址不能超过 500 字").nullable().optional(), parentId: z.string().nullable().optional(), sortOrder: z.number().int().optional(), }); export default defineWrappedResponseHandler(async (event) => { const id = getRouterParam(event, "id"); if (!id) return R.throwError(400, "缺少分类 ID", null); const body = await readBody(event); const data = validate(updateSchema, body); const existing = await getCategoryById(id); if (!existing) return R.throwError(404, "分类不存在", null); const updated = await updateCategory(id, data); await delCache(`category:${id}`); await delCache("categories:tree"); return R.success(updated); });